<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/20 0020
 * Time: 上午 9:04
 */
namespace app\article\model;
use think\console\command\make\Model;
use think\Db;

class Reply extends Model{
    #全部回复列表
    public function reply_list($data){
        #条件
        $where = 'a.del=1';
        $where.= isset($data['keyword'])?' and CONCAT(a.content,a.from_userid) like "%'.$data['keyword'].'%"':'';

        $f = 'a.id,b.content as comment,a.content,a.createtime,c.username as from_user,d.username as to_user';

        #分页
        $total = Db::name('reply')
            ->alias('a')
            ->join('comment b','a.comment_id=b.id')
            ->join('member c','a.from_userid=c.id')
            ->join('member d','a.to_userid=d.id')
            ->where($where)->field($f)->count();
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }

        $list = Db::name('reply')
            ->alias('a')
            ->join('comment b','a.comment_id=b.id')
            ->join('member c','a.from_userid=c.id')
            ->join('member d','a.to_userid=d.id')
            ->field($f)
            ->where($where)->page($data['curr'],$data['limits'])
            ->order(['a.createtime'=>'desc'])->select();
        $res =[
            'list'=>$list,
            'total'=>$total,
            'pages'=>$page,
            'curr'=>$data['curr'],
            'limits'=>$data['limits']
        ];
        return $res;
    }
    #我的回复
    public function myReply_list($data){
        #条件
        $where = 'a.del=1 and a.from_userid='.$data['from_userid'];
        $where.= isset($data['keyword'])?' and CONCAT(a.content,a.from_userid) like "%'.$data['keyword'].'%"':'';

        $f = 'a.id,b.title,a.content,a.createtime,a.reply_id as compose_id,c.username as from_user,d.username as to_user';

        #分页
        $total = Db::name('reply')
            ->alias('a')
            ->join('compose b','a.reply_id=b.id')
            ->join('member c','a.from_userid=c.id')
            ->join('member d','a.to_userid=d.id')
            ->where($where)->field($f)->count();
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }

        $list = Db::name('reply')
            ->alias('a')
            ->join('compose b','a.reply_id=b.id')
            ->join('member c','a.from_userid=c.id')
            ->join('member d','a.to_userid=d.id')
            ->field($f)
            ->where($where)->page($data['curr'],$data['limits'])
            ->order(['a.createtime'=>'desc'])->select();
        $res =[
            'list'=>$list,
            'total'=>$total,
            'pages'=>$page,
            'curr'=>$data['curr'],
            'limits'=>$data['limits']
        ];
        return $res;
    }
    #添加回复
    public function reply_add($data){
        $add = Db::name('reply')->insertGetId($data);
        return $add;
    }
    #删除回复
    public function reply_update($data){
        $del = Db::name('reply')->where([])->update($data);
        return $del;
    }
}